import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import {
	user,
	configs
} from '@/api/entrance.js'
const store = new Vuex.Store({
	state: {
		isLogin: true,
		userInfo: {
			avatar: "",
			create_time: 0,
			currency: 0,
			money:0,
			id: "",
			invitation_code: "",
			isLogin: true,
			last_login_time: 0,
			parent_id: 0,
			phone: "",
			power: 0,
			reward_end_time: 0,
			reward_lord: 0,
			temporary_power: 0,
			username: "",
		}, //用户信息
		currency: 0, //挖矿中资产
		verKey: "node-block", //生成hask密钥key
		power: 0, //当前算力
		isOpenMini: false, //是否开启了挖矿
		miningData: {}, //挖矿实时数据
		reviewVersion:0,//审核版本
		address:{
			"type": "gcj02",
			"altitude": 0,
			"latitude": 0,
			"longitude": 0,
			"speed": 0,
			"accuracy": 0,
			"address": {
				"country": "中国",
				"province": "",
				"city": "北京市",
				"district": "",
				"street": "",
				"streetNum": "",
				"poiName": "",
				"cityCode": ""
			},
			"errMsg": "getLocation:ok"
		},//定位信息
	},
	mutations: {
		//更新登录状态
		setIsLogin_mutation(state, payload) {
			state.isLogin = payload;
		},
		//更新用户信息
		setUserInfo_mutation(state, payload) {
			state.userInfo = payload;
			state.isLogin = payload.isLogin;
			state.power = Number(payload.power)+Number(payload.temporary_power);
		},
		//更新货币
		setCurrency_mutation(state, payload) {
			state.currency = state.currency + Number(payload)
		},
		//直接更新货币为
		setCurrencyDirect_mutation(state, payload) {
			state.currency = payload
		},
		//更新挖矿状态
		setIsOpenMini_mutation(state, payload) {
			state.isOpenMini = payload
		},
		//更新挖矿状态
		setMiningData_mutation(state, payload) {
			state.miningData = payload
		},
		//审核版本
		setReviewVersion_mutation(state, payload) {
			state.reviewVersion = Number(payload)
		},
		//定位信息
		setAddress_mutation(state, payload) {
			state.address = payload
		},
	},
	actions: {
		//异步更新货币
		setCurrency_action({
			commit
		}, payload) {
			commit('setCurrency_mutation', payload)
		},
		//更新用户信息
		setUserInfo_action({commit}) {
			user().then(res => {
				if (res.data) {
					commit('setUserInfo_mutation', res.data)
				} else {
					commit('setIsLogin_mutation', false)
				}
			})
		},
		//更新审核信息
		setReviewVersion_action({commit}){
			configs({type: 'review_version'}).then(res => {
				if (res.data) {
					commit('setReviewVersion_mutation', res.data)
				} else {
					commit('setReviewVersion_mutation', 0)
				}
			})
		}
	}
})
export default store
